/**
 * pixSWX is SWXformat API for pixLib
 *  
 * Copyright © 2007 Laurent Deketelaere, Aral Balkan, Francis Bourre
 * Part of the SWX (SWF Data Exchange Format) project. http://swxformat.org
 * Part of the pixLib project. http://pixlib.org
 *
 * Released under the open source MIT license.
 * http://opensource.org/licenses/mit-license.php
 */
import org.triptyk.pixswx.ISWXListener;
import org.triptyk.pixswx.SWXCall;
import org.triptyk.pixswx.SWXFaultEvent;
import org.triptyk.pixswx.SWXResultEvent;

import com.bourre.commands.Delegate;
import com.bourre.data.libs.LibEvent;
import com.bourre.events.EventType;
import com.bourre.log.PixlibStringifier;
import com.bourre.remoting.RemotingDebug;

class org.triptyk.pixswx.SWXResponder 
	implements ISWXListener
{
	public static var onLoadProgressEVENT : EventType = SWXCall.onLoadProgressEVENT;
	public static var onResultEVENT : EventType = SWXCall.onResultEVENT;
	public static var onFaultEVENT : EventType = SWXCall.onFaultEVENT;
	
	private var _dProgress : Delegate;
	private var _dResult : Delegate;
	private var _dFault : Delegate;

	private var _bProgressListener : Boolean;
	private var _sServiceMethod : String;

	public function SWXResponder( scope, fResult : Function, fFault : Function, fProgress : Function ) 
	{
		if (!fFault) 
		{
			fFault = scope["onFault"];
			if (!fResult) fResult = scope["onResult"];
		}
		
		if ( typeof(fResult) != "function" )
		{
			RemotingDebug.WARN(scope + ".onResult method is undefined, " + this + " won't return any ResultEvent.");
		}
			
		if ( typeof(fFault) != "function" )
		{
			RemotingDebug.WARN(scope + ".onFault method is undefined, " + this + " won't return any FaultEvent.");
		}
		
		if ( typeof(fProgress) != "function" )
		{
			RemotingDebug.WARN(scope + ".onProgress method is undefined, " + this + " won't return any ProgressEvent.");
		} else 
		{
			_dProgress = new Delegate(scope, fProgress);
			_bProgressListener = true;
		}
		
		_dResult = new Delegate(scope, fResult);
		_dFault = new Delegate(scope, fFault);
	}

	public function setMethodName( s : String ) : Void
	{
		_sServiceMethod = s;
	}

	public function getMethodName() : String
	{
		return _sServiceMethod;
	}

	private function _invokeOnProgress( e : LibEvent ) : Void
	{
		_dProgress.setArguments(e);
		_dProgress.execute();
	}

	private function _invokeOnResult( e : SWXResultEvent ) : Void
	{
		_dResult.setArguments(e);
		_dResult.execute();
	}

	private function _invokeOnFault( e : SWXFaultEvent ) : Void
	{
		_dFault.setArguments(e);
		_dFault.execute();
	}

	public function onLoadProgress(e : LibEvent) : Void
	{
		_invokeOnProgress(e);
	}	

	public function onResult(e : SWXResultEvent) : Void
	{
		_invokeOnResult(e);
	}	

	public function onFault(e : SWXFaultEvent) : Void
	{
		_invokeOnFault(e);
	}

	public function onTimeOut(e : LibEvent) : Void
	{
		_invokeOnFault(new SWXFaultEvent(onFaultEVENT, "SWX TimeOut", null, _sServiceMethod));
	}

	public function onLoadInit(e : LibEvent) : Void
	{
		 // -- void
	}

	public function toString() : String 
	{
		return PixlibStringifier.stringify(this);
	}
}